package br.ufla.pgcc.oomeasures.plugin.views;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.part.ViewPart;

import br.ufla.pgcc.oomeasures.ItemMeasured;
import br.ufla.pgcc.oomeasures.measurement.Measurement;


public class SampleView extends ViewPart {

	public static final String ID = "br.ufla.pgcc.oomeasures.plugin.views.SampleView";

	private TreeViewer viewer;
	private ItemMeasured itemsMeasured;

	/**
	 * The constructor.
	 */
	public SampleView() {}

	public ItemMeasured getItemMeasured(){
		return itemsMeasured;
	}

	/**
	 * Shows the given selection in this view.
	 */
	public void showSelection(ISelection selection) {

		if ((!selection.isEmpty()) && (selection instanceof IStructuredSelection)) {
			IJavaElement elem = (IJavaElement) ((IStructuredSelection) selection).getFirstElement();
			if (elem != null) {

				try {
					final IProject project = (IProject) elem.getUnderlyingResource();
					setContentDescription("Project: " + project.getName());
					if (project.isOpen()) {
						Display.getDefault().asyncExec(new Runnable() {
							public void run() {
								try {
									itemsMeasured = createModel(project);
									if (itemsMeasured != null) {
										viewer.setInput(itemsMeasured);
										viewer.refresh(true);
									}
								} catch (CoreException e) {
									e.printStackTrace();
								}
							}

						});
					}
				} catch (JavaModelException e1) {
					e1.printStackTrace();
				}
			}
		}
	}

	/**
	 * This is a callback that will allow us
	 * to create the viewer and initialize it.
	 */
	public void createPartControl(Composite parent) {
		viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		viewer.getTree().setLinesVisible(true);
		viewer.getTree().setHeaderVisible(true);

		TreeViewerColumn column = new TreeViewerColumn(viewer, SWT.NONE);
		column.getColumn().setWidth(200);
		column.getColumn().setMoveable(true);
		column.getColumn().setText("Item");
		column.setLabelProvider(new ColumnLabelProvider() {
			public String getText(Object element) {
				return element.toString();
			}
		});

		column = new TreeViewerColumn(viewer, SWT.NONE);
		column.getColumn().setWidth(100);
		column.getColumn().setMoveable(true);
		column.getColumn().setText("Value");
		column.setLabelProvider(new ColumnLabelProvider() {
			public String getText(Object element) {
				if (element instanceof ItemMeasured) {
					ItemMeasured item = (ItemMeasured)element;
					return Double.toString(item.getValue());
				}
				return "";
			}
		});

		column = new TreeViewerColumn(viewer, SWT.NONE);
		column.getColumn().setWidth(100);
		column.getColumn().setMoveable(true);
		column.getColumn().setText("Description");
		column.setLabelProvider(new ColumnLabelProvider() {
			public String getText(Object element) {
				if (element instanceof ItemMeasured) {
					ItemMeasured item = (ItemMeasured)element;
					if (item.getMeasure() != null) {
						return item.getMeasure().getDescription();
					}
				}
				return "";
			}
		});
		
		viewer.setContentProvider(new MyContentProvider());

	}

	private ItemMeasured createModel(IProject project) throws CoreException {
		Measurement measurement = new Measurement();
		return measurement.execute(project);
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		viewer.getControl().setFocus();
	}

	private class MyContentProvider implements ITreeContentProvider {
		public void dispose() {}
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}

		public Object[] getChildren(Object parentElement) {
			return getElements(parentElement);
		}

		public Object[] getElements(Object inputElement) {
			return ((ItemMeasured) inputElement).getChildren().toArray();
		}

		public Object getParent(Object element) {
			if (element == null) {
				return null;
			}
			return ((ItemMeasured) element).getParent();
		}

		public boolean hasChildren(Object element) {
			return ((ItemMeasured) element).getChildren().size() > 0;
		}
	}
}